package com.healthyeating.manage.service.impl;

import java.util.List;

import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.manage.domain.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.ArticleMapper;
import com.healthyeating.manage.domain.Article;
import com.healthyeating.manage.service.IArticleService;

/**
 * 文章Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-04-08
 */
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 查询文章
     *
     * @param articleId 文章主键
     * @return 文章
     */
    @Override
    public Article selectArticleByArticleId(Long articleId) {
        return articleMapper.selectArticleByArticleId(articleId);
    }

    /**
     * 查询文章列表
     *
     * @param articleVo 文章
     * @return 文章
     */
    @Override
    public List<Article> selectArticleList(ArticleVo articleVo) {
        if (articleVo != null && articleVo.getStatus() != null && articleVo.getStatus().equals("my")) {
            articleVo.setUserId(SecurityUtils.getUserId());
        }
        return articleMapper.selectArticleList(articleVo);
    }

    /**
     * 新增文章
     *
     * @param article 文章
     * @return 结果
     */
    @Override
    public int insertArticle(Article article) {
        article.setCreateTime(DateUtils.getNowDate());
        article.setUserId(SecurityUtils.getUserId());
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章
     *
     * @param article 文章
     * @return 结果
     */
    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章
     *
     * @param articleIds 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleByArticleIds(Long[] articleIds) {
        return articleMapper.deleteArticleByArticleIds(articleIds);
    }

    /**
     * 删除文章信息
     *
     * @param articleId 文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleByArticleId(Long articleId) {
        return articleMapper.deleteArticleByArticleId(articleId);
    }
}
